var express = require('express');
var router = express.Router();
var userDao = require('../dao/userDao');

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
/**
 * 注册逻辑
 */
router.post('/register', function (req, res) {
  var usermess = req.body;
  userDao.userRegister(usermess, function (isRegister) {
      if(isRegister == 1){
          req.session.user = usermess.user;
      }
      res.json({'register':isRegister});
  })
});
/**
 * 登陆逻辑
 */
router.post('/login', function(req, res){
    var usermess = req.body;
    userDao.userLogin(usermess, function (isLogin) {
        //todo 保存登录信息
        if(isLogin == 1){
            req.session.user = req.body.tel;
        }
        res.json({'login':isLogin});
    })
});
/**
 * 获取登录用户的详细信息
 */
router.get('/usermess', function (req, res) {
    var tel = req.session.user;
    userDao.getUserMess(tel, function (result) {
        res.json(result);
    })
});
/**
 * 业主下订单
 */
router.post('/placeorder', function (req, res) {
    var data = req.body;
    if(req.session.user){
        var user = req.session.user;
        data.user = user;
        userDao.placeOrder(data, function (result) {
            res.json(result);
        })
    }else{
        res.json({'mes':'nologin'});
    }
});
/**
 * 免费设计路由
 */
router.post('/freedesigner', function (req, res) {
    var data = req.body;
    if(req.session.user){
        var user = req.session.user;
        data.user = user;
        userDao.freeDesigner(data, function (result) {
            res.json(result);
        })
    }else{
        res.json({'mes':'nologin'});
    }
});
/**
 * 获取订单
 */
router.get('/getOrder', function (req, res) {
    if(req.session.user){
        var user = req.session.user;
        userDao.getOrder(user)
            .then(function (result) {
                res.json(result);
            });
    }else{
        res.json({'mes':'nologin'});
    }
});
/**
 * 评价装修案例
 */
router.post('/assesscase', function (req, res) {
    var data = req.body;
    if(req.session.user){
        var user = req.session.user;
        data.user = user;
        userDao.assessOrder(data)
            .then(function (result) {
                res.json({'mes': result});
            });
    }else{
        res.json({'mes':0});
    }
});
/**
 * 改变用户信息
 */
router.post('/alterUserMess', function (req, res) {
    var mess = req.body;
    userDao.alterUserMess(mess)
        .then(function (result) {
            res.json('ok');
        });
});
/**
 * 装修公司接订单
 */
router.post('/orderTaking', function (req, res) {
    var order = req.body;
    userDao.orderTaking(order)
        .then(function (result) {
            res.json('ok');
        });
});
/**
 * 获取用户的装修日记
 */
router.get('/getDiary', function (req, res) {
    var user = req.session.user;
    userDao.getDiary(user)
        .then(function (result) {
            res.json(result);
        });
});
/**
 * 添加装修日记
 */
router.post('/addDiary', function (req, res) {
    var data = req.body;
    data.user = req.session.user;
    userDao.addDiary(data)
        .then(function (result) {
            res.json(result);
        });
});
/**
 * 获取记账本
 */
router.get('/getCashBook', function (req, res) {
    var user = req.session.user;
    userDao.getCashBook(user)
        .then(function (result) {
            res.json(result);
        });
});
/**
 * 添加记账本
 */
router.post('/addCashBook', function (req, res) {
    var data = req.body;
    data.user = req.session.user;
    userDao.addCashBook(data)
        .then(function (result) {
            res.json(result);
        });
});

module.exports = router;
